package com.yhyvr.api.controller.dictionary;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lz.mybatis.jdbc.auto.dao.BoDictionaryMapper;
import com.lz.mybatis.jdbc.auto.dao.BoGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.BoDictionary;
import com.lz.mybatis.jdbc.auto.model.BoDictionaryExample;
import com.lz.mybatis.jdbc.auto.model.BoGoods;
import com.lz.tool.ListUtils;
import com.yhyvr.api.handle.ApiJsonpView;
import com.yhyvr.api.tool.ViewFactory;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.resp.api.ApiRespData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhangqin on 2017/4/6.
 */

@Controller
public class DictionaryController {

    @Resource
    private BoDictionaryMapper boDictionaryMapper;
    @Resource
    private BoGoodsMapper boGoodsMapper;
    @RequestMapping("/common/dictionary/{key}")
    public @ResponseBody
    ModelAndView getDictionary(@PathVariable String key, String crossDomain){
        BoDictionaryExample boDictionaryExample = new BoDictionaryExample();
        boDictionaryExample.createCriteria().andCodeEqualTo(key);
       List<BoDictionary> list = boDictionaryMapper.selectByExample(boDictionaryExample);
        List<JSONObject> jsonObjectList = Lists.newArrayList();
       if(list!=null&&list.size()>0){
           for (BoDictionary boDictionary : list) {
               JSONObject jsonObject = new JSONObject();
               jsonObject.put("value",boDictionary.getValue());
               jsonObject.put("name",boDictionary.getName());
               jsonObject.put("default",boDictionary.getDefaultValue());
               jsonObjectList.add(jsonObject);
           }
       }
        return ViewFactory.buildApiJsonpView(ApiRespData.buildSucc(jsonObjectList),crossDomain);
    }
    @RequestMapping("/common/dictionary/goods")
    public @ResponseBody
    ModelAndView getGoods(String crossDomain){
        List<BoGoods> list = boGoodsMapper.selectByExample(null);
        List<JSONObject> jsonObjectList = Lists.newArrayList();
        if(list!=null&&list.size()>0){
            for (BoGoods boDictionary : list) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value",boDictionary.getGoodsId());
                jsonObject.put("name",boDictionary.getGoodsTitle());
                jsonObject.put("default",0);
                jsonObjectList.add(jsonObject);
            }
        }
        return ViewFactory.buildApiJsonpView(ApiRespData.buildSucc(jsonObjectList),crossDomain);
    }
}
